var helper = require("../utils/common");
var express = require("express");
var router = express.Router();

module.exports = function(app) {
    // 路由内部全局拦截器
    router.use(function(req, res, next) {
        next();
    });

    // 路由实现
    router.all('/user/:id', function(req, res) {
        var id = req.params.id;
        var reqParams = helper.getParams(req.body);
        var name = reqParams.name;
        res.status(200).json({
            "success": true,
            "value": "hello world, " + name + "("+id+")"
        });
    });

    router.post('/page/signCalendar', function(req, res) {
        res.status(200).json({
            "success": true,
            "value": "1,2,3,5,21,24,30"
        });
    });

    router.post('/page_gallery', function(req, res) {
        var reqParams = helper.getParams(req.body);
        var type = reqParams.type;
        var str = "[{url:\"http://icon.nipic.com/BannerPic/20160824/home/20160824153208.jpg\"},{url:\"http://icon.nipic.com/BannerPic/20160811/home/20160811085257.jpg\"},{url:\"http://icon.nipic.com/BannerPic/20160812/home/20160812130029.jpg\"}]";
        if (type == "pic") {
            res.status(200).json({
                "success": true,
                "value": str
            });
        } else {
            res.status(200).json({
                "success": false
            });
        }

    });

    router.post('/tablecontroltest', function(req, res) {
        var reqParams = helper.getParams(req.body);
        var type = reqParams.type;
        var success = Math.random();
        if (type == "a" && success > 0.5) {
            res.status(200).json({
                "success": false
            });
        } else {
            res.status(200).json({
                "success": true,
                "value": "hello world, 这是页面" + type + "的内容"
            });
        }
    });

    router.post('/types', function(req, res) {
        var reqParams = helper.getParams(req.body);
        var type = reqParams.type;
        var types = "[";
        for (var i = 1; i <= 4; i++) {
            if (i != 4) {
                types += type + i + ",";
            } else {
                types += type + i;
            }
        }
        types += "]";
        res.status(200).json({
            "success": true,
            "value": types
        });
    });
    // mount the router on the app
    app.use('/test', router);
};
